/*#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>

int cells[30][30];
int high=20;
int width=30;
int i,j;

void gotoxy(int x,int y)
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}

void start()
{
	for (i=0;i<high;i++)
		for (j=0;j<width;j++)
		{
			cells[i][j] = rand()%2;
		}
}

void show()
{
	gotoxy(0,0);
	for (i=0;i<=high;i++)
	{
		for (j=0;j<=width;j++)
		{
			if (cells[i][j]==1)
				printf("*");
			else
				printf(" ");
		}
		printf("\n");
	}
	Sleep(150);
}

void input()
{

}

void outinput()
{
    int cells2[20][30];
    int near12;
    for (i=0;i<=high-1;i++)
	{
		for (j=0;j<=width-1;j++)
        {
            near12=cells[i-1][j-1] + cells[i-1][j] + cells[i-1][j+1]
            + cells[i][j-1] + cells[i][j+1] + cells[i+1][j-1] + cells[i+1][j] + cells[i+1][j+1];
            if(near12==3)
                cells2[i][j]=1;
            else if(near12==2)
                cells2[i][j]=cells[i][j];
            else
                cells2[i][j]=0;
        }

	}
	for (i=0;i<=high-1;i++)
		for (j=0;j<=width-1;j++)
            cells[i][j]=cells2[i][j];
}

void main()
{
	start();
	while (1)
	{
	    show();
		input();
		outinput();
	}
}
*/
/*#include <stdio.h>
int main()
{
    float a[21],t=1;
    int n;
    int i,j;
    scanf("%d",&n);
    for(i=0;i<n;i++)
       {
        scanf("%f",&a[i]);
         if(a[i-1]>a[i])
            {
                t=0;
                break;
            }
       }
    if(t!=0)
    {
        scanf("%f",&t);
    for(i=0;i<n;i++)
    {
        if(a[i]>=t)
        {
            for(j=n;j>i;j--)
            {
                a[j]=a[j-1];
            }
            a[i]=t;
            break;
        }
    }
        for(i=0;i<n+1;i++)
        printf("%.2f ",a[i]);
    }
    else
        printf("error");
  return 0;
}
*/
/*#include <stdio.h>
int main()
{
    int a[101][101];
    int n,s=1;
    int i,j;
    scanf("%d",&n);
    for(i=0;i<101;i++)
        {
            for(j=0;j<101;j++)
            a[i][j]=1;
        }
    for(i=0;i<n;i++)
       {
        for(j=0;j<n;j++)
        a[i][j]=0;
       }
     a[0][n-1]=1;
        i=0;
        j=n-1;
    while(s!=(n*n))
    {
        while(a[i+1][j]==0&&(i+1)<=n)
        {
            s++;
            i++;
          a[i][j]=s;
        }
         while(a[i-1][j]==0&&(i-1)>=0)
        {
            s++;
            i--;
          a[i][j]=s;
        }
         while(a[i][j-1]==0&&(j-1)>=0)
        {
            s++;
            j--;
          a[i][j]=s;
        }
        while(a[i][j+1]==0&&(j+1)<=n)
        {
            s++;
            j++;
          a[i][j]=s;
        }
    }
    for(i=0;i<n;i++)
    {
            for(j=0;j<n;j++)
            printf("%d ",a[i][j]);
             printf("\n");
    }
    return 0;
}
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

#define High 15
#define Width 25
#define EnemyNum 10 // 敌机个数


int position_x,position_y; // 飞机位置
int enemy_x[EnemyNum],enemy_y[EnemyNum];  // 敌机位置
int canvas[High][Width] = {0}; // 二维数组存储游戏画布中对应的元素
                        // 0为空格，1为飞机*，2为子弹|，3为敌机@
int goal;
int BulletWidth; // 子弹宽度
int EnemyMoveSpeed; // 敌机移动速度

void gotoxy(int x,int y)  //光标移动到(x,y)位置
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}

void startup()
{
	position_x = High-1;
	position_y = Width/2;
	canvas[position_x][position_y] = 1;
	int k;
	for (k=0;k<EnemyNum;k++)
	{
		enemy_x[k] = rand()%2;
		enemy_y[k] = rand()%Width;
		canvas[enemy_x[k]][enemy_y[k]] = 3;
	}
	goal = 0;
	BulletWidth = 0;
	EnemyMoveSpeed = 20;
}

void show()
{
	gotoxy(0,0);
	int i,j;
	for (i=0;i<High;i++)
	{
		for (j=0;j<Width;j++)
		{
			if (canvas[i][j]==0)
				printf(" ");   //   输出空格
			else if (canvas[i][j]==1)
				    printf("*");//   输出飞机*
			else if (canvas[i][j]==2)
				printf("|");   //   输出子弹|
			else if (canvas[i][j]==3)
				printf("@");   //  输出飞机@
		}
		printf("\n");
	}
	printf("得分：%d\n",goal);
	Sleep(20);
}

void outinput()  // 与用户输入无关的更新
{
	int i,j,k;
	for (i=0;i<High;i++)
	{
		for (j=0;j<Width;j++)
		{
			if (canvas[i][j]==2)
			{
				for (k=0;k<EnemyNum;k++)
				{
					if ((i==enemy_x[k]) && (j==enemy_y[k]))  // 子弹击中敌机
					{
					    printf("\a");
						goal++;                // 分数加1
						if (goal%5==0 && EnemyMoveSpeed>3)   // 达到一定积分后，敌机变快
							EnemyMoveSpeed--;
						if (goal%5==0)   // 达到一定积分后，子弹变厉害
							BulletWidth++;
						canvas[enemy_x[k]][enemy_y[k]] = 0;
						enemy_x[k] = rand()%2;           // 产生新的飞机
						enemy_y[k] = rand()%Width;
						canvas[enemy_x[k]][enemy_y[k]] = 3;
						canvas[i][j] = 0;      // 子弹消失
					}
				}
				// 子弹向上移动
				canvas[i][j] = 0;
				if (i>0)
					canvas[i-1][j] = 2;
			}
		}
	}

	static int speed = 0;
	if (speed<EnemyMoveSpeed)
		speed++;

	for (k=0;k<EnemyNum;k++)
	{
		if ((position_x==enemy_x[k]) && (position_y==enemy_y[k]))
		{
			printf("啧啧啧，还是回去继续修炼吧！\n");
			Sleep(3000);
			system("pause");
			exit(0);
		}

		if (enemy_x[k]>High)   // 敌机跑出显示屏幕
		{
			canvas[enemy_x[k]][enemy_y[k]] = 0;
			enemy_x[k] = rand()%2;           // 产生新的飞机
			enemy_y[k] = rand()%Width;
			canvas[enemy_x[k]][enemy_y[k]] = 3;
			goal--;  // 减分
		}

		if (speed == EnemyMoveSpeed)
		{
			// 敌机下落
			for (k=0;k<EnemyNum;k++)
			{
				canvas[enemy_x[k]][enemy_y[k]] = 0;
				enemy_x[k]++;
				speed = 0;
				canvas[enemy_x[k]][enemy_y[k]] = 3;
			}
		}
	}
}

void input()  // 与用户输入有关的更新
{
	char input;
	if(kbhit())  // 判断是否有输入
	{
		input = getch();  // 根据用户的不同输入来移动，不必输入回车
		if (input == 'a' && position_y>0)
		{
			canvas[position_x][position_y] = 0;
			position_y--;  // 位置左移
			canvas[position_x][position_y] = 1;
		}
		else if (input == 'd' && position_y<Width-1)
		{
			canvas[position_x][position_y] = 0;
			position_y++;  // 位置右移
			canvas[position_x][position_y] = 1;
		}
		else if (input == 'w')
		{
			canvas[position_x][position_y] = 0;
			position_x--;  // 位置上移
			canvas[position_x][position_y] = 1;
		}
		else if (input == 's')
		{
			canvas[position_x][position_y] = 0;
			position_x++;  // 位置下移
			canvas[position_x][position_y] = 1;
		}
		else if (input == ' ')  // 发射子弹
		{
			int left = position_y-BulletWidth;
			int right = position_y+BulletWidth;
			if (left<0)
				left = 0;
			if (right>Width-1)
				right = Width-1;
			int k;
			for (k=left;k<=right;k++) // 发射闪弹
				canvas[position_x-1][k] = 2; // 发射子弹的初始位置在飞机的正上方
		}
	}
}

int main()
{
	startup();
	while (1)
	{
		show();
		input();
        outinput();
	}
	return 0;
}













